<?php

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
 	require_once (APPLICATION_PATH . "\\models\\connectionDb.php");
} else {
    require_once (APPLICATION_PATH . "//models//connectionDb.php");
}

class Application_Model_DbTable_Interestinfosport extends Zend_Db_Table_Abstract
{

    protected $_name = 'InterestInfo_Sport';
    protected $_primary = 'id';

    public function getUserSports($userInfoID)
    {
    	$userInfoID = (int)$userInfoID;
    	$rows = $this->fetchAll("User_id = " . $userInfoID);
    	return $rows;
    }
    
    public function addSport($infoID, $sportID)
    {
    	$data = array(
    		'Sport_id' => $sportID,
    		'InterestInfo_id' => $infoID,
    	);
    	$this->insert($data);
    }

    public function removeSport($infoID, $sportID)
    {
    	$where = "InterestInfo_id = " . $infoID . " AND Sport_id = " . $sportID;
    	$this->delete($where);
    }
    
	public function getSportsForUser($InterestInfo_id)
    {
    	try{
    		
    		$sql ="select InterestInfo_Sport.*, Sports.Name";
			$sql .=" from InterestInfo_Sport";
			$sql .=" left join Sports on Sports.id = InterestInfo_Sport.Sport_id";
			$sql .=" where 1=1";			
			$sql .=" and InterestInfo_id = ".$InterestInfo_id;    		 
    		$db = connectionDb::connectDB();		
			$rows = $db->fetchAll($sql);			
	  					 
	    	if (!$rows) {
	   			return null;
	  		}
	  		return $rows;
    	
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }
    }
	public function getSportsForUserNotAdded($InterestInfo_id)
    {
    	try{
    		
    		$sql ="select Sports.id as 'Sport_id',Sports.Name";
			$sql .=" from Sports ";
			$sql .=" where Sports.id NOT IN( select InterestInfo_Sport.Sport_id";
			$sql .=" from Sports";			
			$sql .=" inner join InterestInfo_Sport on InterestInfo_Sport.Sport_id = Sports.id";  
			$sql .=" where InterestInfo_id =". $InterestInfo_id .")";  		 
    		$db = connectionDb::connectDB();		
			$rows = $db->fetchAll($sql);			
	  					 
	    	if (!$rows) {
	   			return null;
	  		}
	  		return $rows;
    	
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }
    
    }
    
	public function deleteSportForUser($id)
    {	
    	try {
    		$this->delete('id = ' . (int)$id);
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        } 
    	
    }
    
    
}

